//
//  WBMiddleView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/13.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit
import SnapKit

class WBMiddleView: UIView {
    
    var middleViewBottomConstraint:Constraint?
    
    var statusViewModel: WBStatusViewModel?{
        
        
        didSet{
            
            textLabel.attributedText = statusViewModel?.retweetAttr
            //判断有没有配图
            //卸载约束
            middleViewBottomConstraint?.uninstall()
            if statusViewModel?.statusModel?.retweeted_status?.pic_urls?.count > 0{
                
                //有配图
                //更新约束
                self.snp_makeConstraints(closure: { (make) in
                   middleViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                //赋值
                pictureView.pictureInfo = statusViewModel?.statusModel?.retweeted_status?.pic_urls
                //显示配图
                pictureView.hidden = false
                
            }else
            {
                //没有配图
                //更新约束
                self.snp_makeConstraints(closure: { (make) in
                   middleViewBottomConstraint = make.bottom.equalTo(textLabel).offset(10).constraint
                })
                //隐藏配图
                pictureView.hidden = true
            }
            
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    func setupUI() {
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        //添加控件
        addSubview(textLabel)
        addSubview(pictureView)
        
        //设置约束
        textLabel.snp_makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(10)
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.leading.equalTo((ScreenW - 250) * 0.5)
        }
        //自动设置高度用
        self.snp_makeConstraints { (make) in
            middleViewBottomConstraint = make.bottom.equalTo(textLabel).offset(10).constraint
        }
    }
    //懒加载配图
    private lazy var pictureView: WBStatusPictureView = WBStatusPictureView()
    
    //懒加载正文内容的label
    private lazy var textLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: 15, maxWidth: ScreenW - 20)
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
